from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from datetime import datetime
from django.http import Http404, HttpResponseRedirect
from trafficlight.blogs.models import *
from trafficlight.blogs.forms import *

@login_required
def post(request, blog_url):
    blog = get_object_or_404(Blog, url_name=blog_url)
    if request.user != blog.owner :
        raise Http404
    if request.method == 'POST':
        form = EntryForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            title = cd['title']
            text = cd['text']
            post_datetime = datetime.now()
            entry = Entry(blog=blog,
                title=title,
                text=text,
                datetime=post_datetime)
            entry.save()
            return HttpResponseRedirect('/blogs/{0}/entry/{1}'.format(blog_url,
                str(entry.id)))
        else:
            return render_to_response('blogs/post.html', {
                'form' : form, 'blogname' : blog.name })
    else:
        form = EntryForm()
        return render_to_response('blogs/post.html', { 'form' : form, 'blog' : blog }, context_instance=RequestContext(request))
            
@login_required 
def edit(request, blog_url, entry_id):
    #implementation of edit blog entry
    blog = get_object_or_404(Blog, url_name=blog_url)
    if request.user != blog.owner :
        raise Http404
    entry = get_object_or_404(Entry, id=entry_id)
    if request.method == 'POST' :
        entry.title = request.POST['title']
        entry.text = request.POST['text']
        entry.datetime = datetime.now()
        entry.save()
        return HttpResponseRedirect('/blogs/{0}/entry/{1}/'.format(blog_url, str(entry_id)))
    else:
        form = EntryForm( { 'text' : entry.text,
                          'title' : entry.title, 
                          'datetime' : datetime.now() 
                          }
                        )
        return render_to_response(
            'blogs/edit.html', 
            { 'form' : form, 'entry' : entry},
            context_instance=RequestContext(request))

@login_required
def delete(request, blog_url, entry_id):
    blog = get_object_or_404(Blog, url_name=blog_url)
    if request.user != blog.owner : 
        raise Http404
    entry = get_object_or_404(Entry, id=entry_id)
    entry.delete()
    return HttpResponseRedirect('/blogs/{0}/'.format(blog_url))

@login_required
def view(request, blog_url, entry_id):
    #implementation of view blog
    blog = get_object_or_404(Blog, url_name=blog_url)
    entry = get_object_or_404(Entry, id=entry_id)
    return render_to_response('blogs/view.html', { 'entry' : entry }, context_instance=RequestContext(request))
    

@login_required
def index(request, blog_url):
    #implementation of blog's recent entries
    blog = get_object_or_404(Blog, url_name=blog_url)
    entries = blog.entry_set.order_by('-datetime')[0:10]    
    return render_to_response('blogs/index.html', 
                              { 'entries' : entries, 'blog' : blog },
                              context_instance=RequestContext(request))

@login_required
def comment_delete( request, comment_id ):
    #delete comment
    comment = get_object_or_404( Comment, id=comment_id )
    if comment.commenter == request.user or comment.entry.blog.owner == request.user :
        comment.delete()
        return HttpResponseRedirect( '/blogs/{0}/entry/{1}/'.format( comment.entry.blog.url_name,
            comment.entry.id) )
    else:
        raise Http404

@login_required
def comment_post( request, entry_id ):
    #post a comment
    entry = get_object_or_404( Entry, id=entry_id )
    if request.method == 'POST' :
        form = CommentForm( request.POST )
        if form.is_valid() :
            cd = form.cleaned_data
            comment = Comment( commenter=request.user, entry=entry,
                              title=cd['title'], text=cd['text'],
                              datetime=datetime.now() )
            comment.save()
            return HttpResponseRedirect( '/blogs/{0}/entry/{1}'.format(
                comment.entry.blog.url_name, comment.entry.id ) )
        else:
            return render_to_response( 
                'blogs/comments/post.html', { 'form' : form, 'entry' : entry },
                context_instance=RequestContext(request) )
    else:
        form = CommentForm()
        return render_to_response( 
            'blogs/comments/post.html', { 'form' : form, 'entry' : entry },
            context_instance=RequestContext(request) )
